package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduCertificateMapper;
import com.zenith.edu.po.domain.EduCertificate;
import com.zenith.edu.po.dto.EduCertificateDTO;
import com.zenith.edu.po.vo.EduCertificateVO;
import com.zenith.edu.service.EduCertificateService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduCertificateServiceImpl extends ServiceImpl<EduCertificateMapper, EduCertificate> implements EduCertificateService {
    
    @Override
    public EduCertificateVO createCertificate(EduCertificateDTO dto) {
        EduCertificate certificate = new EduCertificate();
        BeanUtils.copyProperties(dto, certificate);
        this.save(certificate);
        
        EduCertificateVO vo = new EduCertificateVO();
        BeanUtils.copyProperties(certificate, vo);
        return vo;
    }
    
    @Override
    public EduCertificateVO getCertificateById(Long id) {
        EduCertificate certificate = this.getById(id);
        if (certificate == null) {
            return null;
        }
        
        EduCertificateVO vo = new EduCertificateVO();
        BeanUtils.copyProperties(certificate, vo);
        return vo;
    }
    
    @Override
    public EduCertificateVO updateCertificate(EduCertificateDTO dto) {
        EduCertificate certificate = new EduCertificate();
        BeanUtils.copyProperties(dto, certificate);
        this.updateById(certificate);
        
        EduCertificateVO vo = new EduCertificateVO();
        BeanUtils.copyProperties(certificate, vo);
        return vo;
    }
    
    @Override
    public boolean deleteCertificate(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduCertificateVO> getCertificateList() {
        List<EduCertificate> certificateList = this.list();
        return certificateList.stream().map(certificate -> {
            EduCertificateVO vo = new EduCertificateVO();
            BeanUtils.copyProperties(certificate, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}